<?php
namespace app\agentmobile\controller;

use app\agentmobile\model\User;
use app\lib\exception\LoginException;
use app\lib\exception\SuccessMessage;
use app\lib\validate\UserLogin;
use think\Controller;
use think\Request;
use app\agentmobile\model\Wxuser;
use app\lib\exception\WeChatException;
use Wx\Wechat;
use app\agent\model\Agent;
class Login extends Controller{

    public function Index() {

        return view();
    }
    public function loginmobile() {

        return view();
    }
    //登录
    public function login() {
        (new UserLogin())->goCheck();
        $data = Request::instance()->post();
        $res = $this->loginApi($data['mobile'],md5($data['password']));
        if($res['respCode'] == 006){
            throw new LoginException([
                'msg' => '该账户不存在，请注册',
                'errorCode'=>-1
            ]);
        }elseif($res['respCode'] !== '000'){
            throw new LoginException([
                'msg' => $res['respDesc']
            ]);
        }
        //注册用户
        $data['name'] = $res['merName'];
        $data['merId'] = $res['merId'];
        $userObj = User::create($data,true);
        Wxuser::update(['uid' => $userObj->id],['id' => session("wxId")]);
       
        session('uid', $userObj->id);
        throw new SuccessMessage();

    }
    //登录接口
    public function loginApi($mobile='',$pwd='')
    {
        $arr = [];
        $arr['agentId'] =  config('api.agentId');
        $arr['loginId'] = $mobile;
        $arr['loginPwd'] = $pwd;
        $arr['appType'] = "android";
        $arr['clientModel'] = 111;
        $url = config('api.reqUrl').'ss/login.do';
        $resdata = curl_form($url,$arr);
        return json_decode(mb_convert_encoding($resdata,'UTF-8','GBK'),true);
    }
    //退出
    public function logout() {
        session('uid', null);
        session('agentId', null);
        session('agentMobile', null);
        $this->redirect('Login/index');
    }

    //微信授权
    public function wechat() {
        $app_id = config('wx.app_id');
        $app_secret = config('wx.app_secret');
        $url = url('agentmobile/Login/getUserInfo','',true,true);
        $res = Wechat::instance($app_id, $app_secret)->getBaseInfo($url);
    }

    // 微信获取用户信息
    public function getUserInfo() {
        $app_id = config('wx.app_id');
        $app_secret = config('wx.app_secret');
        $code = $_GET['code'];
        $data = Wechat::instance($app_id,$app_secret)->getUserInfo($code);
        $userdata = Wxuser::where(array('openid' => $data['openid']))->find(); // 查询是否已经注册

        if(!$userdata ){//没有注册
            $wxObj = Wxuser::create($data,true);
            session("wxId",$wxObj->id);
            $this->redirect('loginmobile');

        }else{
            //已经微信注册过的用户,没有绑定App
            if($userdata['uid'] == 0){
                session("wxId",$userdata['id']);
                $this->redirect('loginmobile');
            }else{//已经绑定App
                session('uid', $userdata['uid']);
                $this->redirect('Index/index');
            }

        }

    }

}